package com.lechuang.order.mq.listener;


import com.lechuang.mq.core.BaseRabbiMqHandler;
import com.lechuang.mq.listenter.MqListener;
import com.lechuang.order.mq.MqConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

/**
 * <p>
 *     mq监听
 * </p>
 * @author liaoningbo
 * @since 2024/1/8
 */
@Log4j2
@Component
public class TestListener extends BaseRabbiMqHandler<String> {
    /**
     * <p>
     *     接收普通消息
     * </p>
     * @author liaoningbo
     * @since 2024/1/8
     */
    @RabbitListener(queues = MqConstant.MQ_QUEUE)
    public void mqQueue(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Message message) {
        super.onMessage(msg, deliveryTag, channel, message, new MqListener<String>() {
            @Override
            public void handler(String msg, Channel channel) {
               log.info("mqQueue 接收到的消息:{}", msg);
            }
        });
    }

    /**
     * <p>
     *     延迟消息
     * </p>
     * @author liaoningbo
     * @since 2024/1/8
     */
    @RabbitListener(queues = MqConstant.MQ_DELAYED_QUEUE)
    public void delayQueue(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Message message) {
        super.onMessage(msg, deliveryTag, channel, message, new MqListener<String>() {
            @Override
            public void handler(String msg, Channel channel) {
                log.info("delayQueue 接收到的消息:{}", msg);
            }
        });
    }

    /**
     * <p>
     *     广播消息--正常消费
     * </p>
     * @author liaoningbo
     * @since 2024/1/8
     */
    @RabbitListener(queues = MqConstant.MQ_BROADCAST_QUEUE1)
    public void broadcastQueue1(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Message message) {
        super.onMessage(msg, deliveryTag, channel, message, new MqListener<String>() {
            @Override
            public void handler(String msg, Channel channel) {
                log.info("broadcastQueue1 接收到的消息:{}", msg);
            }
        });
    }

    /**
     * <p>
     *     广播消息，消费异常
     * </p>
     * @author liaoningbo
     * @since 2024/1/8
     */
    @RabbitListener(queues = MqConstant.MQ_BROADCAST_QUEUE2)
    public void broadcastQueue2(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Message message) {
        super.onMessage(msg, deliveryTag, channel, message, new MqListener<String>() {
            @Override
            public void handler(String msg, Channel channel) {
                log.info("broadcastQueue2 接收到的消息:{}", msg);
            }
        });
    }
}
